package com.ikimuhendis.ldrawer.sample.slice;

import com.ikimuhendis.ldrawer.DrawerArrowDrawable;
import com.ikimuhendis.ldrawer.sample.ResourceTable;
import com.mikepenz.materialdrawer.interfaces.OnDrawerCloseListener;
import com.mikepenz.materialdrawer.interfaces.OnDrawerOpenListener;
import com.mikepenz.materialdrawer.interfaces.OnDrawerScrollListener;
import com.mikepenz.materialdrawer.widget.MaterialDrawerSliderView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

/**
 * MainAbilitySlice
 *
 * @since 2021-07-12
 */
public class MainAbilitySlice extends AbilitySlice
        implements Component.ClickedListener, ListContainer.ItemClickedListener {
    private static final String HOME = "home";
    private static final String BACK = "back";
    private static final String START = "start";
    private static final String COLOR = "color";
    private static final String COLOR_WHITE = "white";
    private static final int INT_2 = 2;
    private static final int INT_3 = 3;
    private static final int INT_4 = 4;
    private static final int INT_5 = 5;
    private static final int INT_6 = 6;
    private static final int INT_50 = 50;
    private static final int INT_180 = 180;
    private static final int INT_200 = 200;
    String[] values = new String[]{
        "Stop Animation (Back icon)",
        "Stop Animation (Home icon)",
        "Start Animation",
        "Change Color",
        "GitHub Page",
        "Share",
        "Rate"
    };
    private DependentLayout bt;
    private MaterialDrawerSliderView mDrawerLayout;
    private DirectionalLayout dependentLayout;
    private ListContainer mListContainer;
    private Component handle;
    private Image ig;
    private DrawerArrowDrawable drawerArrow;
    /**
     * 添加动画效果
     */
    private AnimatorProperty mAnimatorIcon;
    private boolean isAnimatorIcon;
    private String mState = START;
    private String mColor = "";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    /**
     * initView
     */
    private void initView() {
        ig = (Image) findComponentById(ResourceTable.Id_ig);
        bt = (DependentLayout) findComponentById(ResourceTable.Id_bt);
        mDrawerLayout = (MaterialDrawerSliderView) findComponentById(ResourceTable.Id_slide);
        dependentLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_slide_layout);
        Display display = DisplayManager.getInstance().getDefaultDisplay(this).get();
        dependentLayout.setWidth(display.getAttributes().width - display.getAttributes().width / INT_3);
        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_navdrawer);
        handle = findComponentById(ResourceTable.Id_handle);
        handle.setAlpha(0);
        mDrawerLayout.setOnDrawerCloseListener(new OnDrawerCloseListener() {
            @Override
            public void onDrawerClosed() {
                handle.setAlpha(0);
                if (!isAnimatorIcon) {
                    ig.setPixelMap(ResourceTable.Media_caidan);
                }
                if (mColor.equals(COLOR) && !isAnimatorIcon) {
                    ig.setPixelMap(ResourceTable.Media_color_caidan);
                }
            }
        });
        mDrawerLayout.setOnDrawerOpenListener(new OnDrawerOpenListener() {
            @Override
            public void onDrawerOpened(float v) {
                handle.setAlpha(INT_50);
                if (!isAnimatorIcon) {
                    ig.setPixelMap(ResourceTable.Media_back);
                }
                if (mColor.equals(COLOR) && !isAnimatorIcon) {
                    ig.setPixelMap(ResourceTable.Media_color_back);
                }
            }
        });
        mDrawerLayout.setOnDrawerScrollListener(new OnDrawerScrollListener() {
            @Override
            public void onScrollStarted() {
                if (!isAnimatorIcon) {
                    rotateAnimator();
                }
            }

            @Override
            public void onScrollEnded() {
            }
        });

        bt.setClickedListener(this);
        handle.setClickedListener(this);

        mListContainer.setItemProvider(new MypAdapter());
        mListContainer.setItemClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_bt:
                if (!isAnimatorIcon) {
                    rotateAnimator();
                }
                if (mDrawerLayout.isOpened()) {
                    mDrawerLayout.animateClose();
                    handle.setAlpha(0);
                } else {
                    mDrawerLayout.animateOpen();
                    handle.setAlpha(INT_50);
                }
                if (mState.equals(HOME)) {
                    ig.setPixelMap(ResourceTable.Media_caidan);
                } else if (mState.equals(BACK)) {
                    ig.setPixelMap(ResourceTable.Media_back);
                }
                if (mColor.equals(COLOR) && mState.equals(HOME)) {
                    ig.setPixelMap(ResourceTable.Media_color_caidan);
                } else if (mColor.equals(COLOR) && mState.equals(BACK)) {
                    ig.setPixelMap(ResourceTable.Media_color_back);
                }
                break;
            case ResourceTable.Id_handle:
                if (mDrawerLayout.isOpened()) {
                    mDrawerLayout.animateClose();
                    rotateAnimator();
                }
                break;
            default:
                break;
        }
    }

    private void rotateAnimator() {
        if (mAnimatorIcon != null) {
            mAnimatorIcon.reset();
        }
        mAnimatorIcon = ig.createAnimatorProperty();
        mAnimatorIcon.rotate(INT_180).setDuration(INT_200);
        mAnimatorIcon.start();
    }

    @Override
    public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
        switch (i) {
            case 0:
                isAnimatorIcon = true;
                if (mColor.equals(COLOR)) {
                    ig.setPixelMap(ResourceTable.Media_color_back);
                } else {
                    ig.setPixelMap(ResourceTable.Media_back);
                }
                mState = BACK;
                break;
            case 1:
                if (mColor.equals(COLOR)) {
                    ig.setPixelMap(ResourceTable.Media_color_caidan);
                } else {
                    ig.setPixelMap(ResourceTable.Media_caidan);
                }
                isAnimatorIcon = true;
                mState = HOME;
                break;
            case INT_2:
                isAnimatorIcon = false;
                if (mColor.equals(COLOR)) {
                    ig.setPixelMap(ResourceTable.Media_color_back);
                } else {
                    ig.setPixelMap(ResourceTable.Media_back);
                }
                mState = START;
                break;
            case INT_3:
                if (mState.equals(BACK)) {
                    ig.setPixelMap(ResourceTable.Media_color_back);
                    mColor = COLOR;
                    return;
                } else if (mState.equals(HOME)) {
                    ig.setPixelMap(ResourceTable.Media_color_caidan);
                    mColor = COLOR;
                    return;
                } else if (mColor.equals(COLOR)) {
                    ig.setPixelMap(ResourceTable.Media_back);
                    mColor = COLOR_WHITE;
                    return;
                } else {
                    ig.setPixelMap(ResourceTable.Media_color_back);
                    mColor = COLOR;
                    return;
                }
            case INT_4:
                Intent intentsFour = new Intent();
                Operation operationFour = new Intent.OperationBuilder()
                        .withUri(Uri.parse("https://github.com/IkiMuhendis/LDrawer"))
                        .withAction(IntentConstants.ACTION_SEARCH)
                        .build();
                intentsFour.setOperation(operationFour);
                startAbility(intentsFour);
                break;
            case INT_5:
                new ToastDialog(getContext()).setText("暂不支持该功能").show();
                break;
            case INT_6:
                String appUrl = "https://play.google.com/store/apps/details?id=" + getBundleName();
                Intent intentsSix = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withUri(Uri.parse(appUrl))
                        .withAction(IntentConstants.ACTION_SEARCH)
                        .build();
                intentsSix.setOperation(operation);
                startAbility(intentsSix);
                break;
            default:
                break;
        }
    }

    /**
     * MypAdapter
     *
     * @since 2021-07-12
     */
    class MypAdapter extends BaseItemProvider {
        @Override
        public int getCount() {
            return values.length;
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
            Component parse = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_simple_list_item_1,
                    null, false);
            Text text = (Text) parse.findComponentById(ResourceTable.Id_text1);
            text.setText(values[i]);
            return parse;
        }
    }
}
